package com.example.koizzi.ad20180517_news.adepter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.koizzi.ad20180517_news.bean.NewsType;
import com.example.koizzi.ad20180517_news.R;

import java.util.List;

public class HoriAdater  extends RecyclerView.Adapter<HoriAdater.ViewHolder>{
    private List<NewsType> hlistType;
    private Context context;
    private OnClickListener listener;
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvBname;
        View HView;
        public ViewHolder(View itemView) {
            super(itemView);
            HView = itemView;
            tvBname = itemView.findViewById(R.id.tvbbname);
        }
    }

    public HoriAdater(List<NewsType> hlistType, Context context) {
        this.hlistType = hlistType;
        this.context = context;
    }

    @NonNull
    @Override
    public HoriAdater.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hori_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final HoriAdater.ViewHolder holder, int position) {
        final NewsType nType = hlistType.get(position);
        holder.tvBname.setText(nType.getType());

        holder.tvBname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener = (OnClickListener) context;
                listener.change(nType.getTid());
            }
        });
    }

    @Override
    public int getItemCount() {
        return hlistType.size();
    }

    public interface OnClickListener{
        public void change(String tid);
    }
}
